package com.mobilebsc.httpclient.callback;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.mobilebsc.datatype.KPI;

public class KPIReferencedByX_HttpResponseHandler extends
		KPIHttpResponseHandler {

	private static final String TAG = "KPIReferencedByX_HttpResponseHandler";

	public KPIReferencedByX_HttpResponseHandler(
			IAsyncTaskCompleteListener<List<KPI>> callback) {
		super(callback);
	}

	@Override
	public void onSuccess(JSONArray referencedKPI) {

		Log.i(TAG, "onSuccess0 = JSON length " + referencedKPI.length()
				+ ", JSON string " + referencedKPI.toString());

		kpiList = new ArrayList<KPI>();
		JSONObject kpiJSON = new JSONObject();

		if (referencedKPI.length() == 0) {
			kpiList.add(new KPI());
		} else {
			for (int i = 0; i < referencedKPI.length(); i++) {
				try {
					kpiJSON = referencedKPI.getJSONObject(i).optJSONObject(
							"kpi");
					KPI kpi = new KPI(kpiJSON);
					kpiList.add(kpi);
				} catch (JSONException e) {
					Log.e(TAG, "onSuccess = " + e.toString());
				}
			}
		}
	}
}